package my.edu.model.bills.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import my.edu.common.constant.MessageConstant;
import my.edu.common.domain.PageDTO;
import my.edu.common.exception.Order.PurchaseException;
import my.edu.model.bills.purchase.entity.PurchaseOrder;
import my.edu.model.bills.purchase.entity.PurchaseOrderDetail;
import my.edu.model.bills.purchase.entity.dto.PurchaseDetailOrderDTO;
import my.edu.model.bills.purchase.entity.dto.PurchaseOrderSearchDTO;
import my.edu.model.bills.purchase.entity.vo.PurchaseDetailOrderVO;
import my.edu.model.bills.purchase.entity.vo.PurchaseOrderVO;
import my.edu.model.bills.purchase.mapper.PurchaseOrderDetailMapper;
import my.edu.model.commodity.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import java.math.BigDecimal;
import java.util.List;

@Service
public class PurchaseOrderDetailServiceImpl extends ServiceImpl<PurchaseOrderDetailMapper, PurchaseOrderDetail> implements IPurchaseOrderDetailService {

    @Autowired
    private IProductService productService;

    @Override
    @Transactional
    public void saveBatchByDTO(List<PurchaseDetailOrderDTO> purchaseDetailDTOList, Long id) {
        List<PurchaseOrderDetail> purchaseOrderDetails = purchaseDetailDTOList.stream().map(i -> {
            PurchaseOrderDetail detail = BeanUtil.copyProperties(i, PurchaseOrderDetail.class,"id");
            detail.setPurchaseOrderId(id);
            return detail;
        }).toList();
        saveBatch(purchaseOrderDetails);
    }

    @Override
    @Transactional
    public void updateBatchByDTO(List<PurchaseDetailOrderDTO> purchaseDetailDTOList, Long id) {
        remove(Wrappers.<PurchaseOrderDetail>lambdaQuery().eq(PurchaseOrderDetail::getPurchaseOrderId,id));
        saveBatchByDTO(purchaseDetailDTOList,id);
    }

    @Override

    public void getDetail(List<PurchaseOrderVO> records) {
        for (PurchaseOrderVO purchaseOrderVO : records) {
           getDetail(purchaseOrderVO,purchaseOrderVO.getId());
        }
    }

    @Override
    public List<PurchaseOrderDetail> getReportList( PurchaseOrderSearchDTO purchaseOrderSearchDTO) {
        MPJLambdaWrapper<PurchaseOrderDetail> wrapper = new MPJLambdaWrapper<PurchaseOrderDetail>()
                .selectAll(PurchaseOrderDetail.class)
                .leftJoin(PurchaseOrder.class, PurchaseOrder::getId, PurchaseOrderDetail::getPurchaseOrderId)
                .between(purchaseOrderSearchDTO.getBeginTime() != null,
                        PurchaseOrder::getOrderDate, purchaseOrderSearchDTO.getBeginTime(), purchaseOrderSearchDTO.getEndTime())
                .eq(purchaseOrderSearchDTO.getSupplierId()!= null,
                        PurchaseOrder::getSupplierId, purchaseOrderSearchDTO.getSupplierId())
                .and(StringUtils.hasText(purchaseOrderSearchDTO.getMaterialParam()),i->i
                        .like(PurchaseOrderDetail::getProductNo, purchaseOrderSearchDTO.getMaterialParam())
                        .or()
                        .like(PurchaseOrderDetail::getProductName, purchaseOrderSearchDTO.getMaterialParam()));
        List<PurchaseOrderDetail> purchaseOrderDetails = baseMapper.selectJoinList(wrapper);
        return purchaseOrderDetails;
    }

    @Async
    protected void getDetail(PurchaseOrderVO purchaseOrderVO, Long id) {
        purchaseOrderVO.setPurchaseDetailDTOList(
                lambdaQuery()
                        .eq(PurchaseOrderDetail::getPurchaseOrderId, id).list()
                        .stream().map(i->BeanUtil.copyProperties(i, PurchaseDetailOrderVO.class)).toList());
    }


}
